package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.MyEnum.ConsumptionEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private ConsumptionMapper consumptionMapper;


    @Override
    public void save(Appointment appointment) {
        //添加流水号
        appointment.setAno(DateUtil.format(new Date(),"yyyyMMddHHmmss")+ RandomUtil.randomNumbers(5));
        //添加创建时间
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> listAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public PageInfo<Appointment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"status,create_time"); //对下一句sql进行自动分页
        List<Appointment> appointments = appointmentMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Appointment>(appointments);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        appointmentMapper.updateStatus(id,status);
    }

    @Override
    public void foundConsumption(String ano) {
        //根据流水号查出预约单
        Appointment appointment = appointmentMapper.selectByAno(ano);
        //将预约单中的数据存存到消费单对象中
        Consumption consumption = new Consumption();
        consumption.setAppointmentAno(ano);
        consumption.setCreateTime(new Date());
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        consumption.setBusiness(appointment.getBusiness());
        consumption.setCheckinTime(new Date());
        consumption.setInfo(appointment.getInfo());
        consumption.setStatus(ConsumptionEnum.CONSUME.getValue());
        consumption.setCno(DateUtil.format(new Date(),"yyyyMMdd")+ RandomUtil.randomNumbers(5));
        consumptionMapper.insert(consumption);
    }
}
